from django import forms
from django.urls import reverse_lazy

import dal_queryset_sequence
import dal_select2_queryset_sequence
from dal import autocomplete

from .models import EquipmentItem, EquipmentList, PowerSupplyModel, NetworkDeviceModel, SeismicEquipmentEntity


class EquipmentItemForm(autocomplete.FutureModelForm):
    equipment = dal_queryset_sequence.fields.QuerySetSequenceModelField(
        label='其它设备列表',
        queryset=autocomplete.QuerySetSequence(
            PowerSupplyModel.objects.all(),
            NetworkDeviceModel.objects.all(),
        ),
        required=True,
        widget=dal_select2_queryset_sequence.widgets.QuerySetSequenceSelect2('equipment:equipment-autocomplete'),
    )

    class Meta:
        model = EquipmentItem
        fields = ('equipment', 'quantity',)


EquipmentItemFormSet = forms.inlineformset_factory(EquipmentList, EquipmentItem, form=EquipmentItemForm, extra=2,
                                                   max_num=3)


class PowerSuppliesItemForm(autocomplete.FutureModelForm):
    equipment = dal_queryset_sequence.fields.QuerySetSequenceModelField(
        label='供电设备',
        queryset=autocomplete.QuerySetSequence(
            PowerSupplyModel.objects.all(),
        ),
        required=True,
        widget=dal_select2_queryset_sequence.widgets.QuerySetSequenceSelect2('equipment:powersuppliese-autocomplete'),
    )

    class Meta:
        model = EquipmentItem
        fields = ('equipment', 'quantity',)


PowerSuppliesItemFormSet = forms.inlineformset_factory(EquipmentList, EquipmentItem, form=PowerSuppliesItemForm,
                                                       extra=2,
                                                       max_num=3)


class NetworkDevicesItemForm(autocomplete.FutureModelForm):
    equipment = dal_queryset_sequence.fields.QuerySetSequenceModelField(
        label='通讯设备',
        queryset=autocomplete.QuerySetSequence(
            NetworkDeviceModel.objects.all(),
        ),
        required=True,
        widget=dal_select2_queryset_sequence.widgets.QuerySetSequenceSelect2('equipment:networkdevices-autocomplete'),
    )

    class Meta:
        model = EquipmentItem
        fields = ('equipment', 'quantity',)


NetworkDevicesItemFormSet = forms.inlineformset_factory(EquipmentList, EquipmentItem, form=NetworkDevicesItemForm,
                                                        extra=1,
                                                        max_num=2)


class EquipmentListForm(forms.ModelForm):
    seismic_equipment_entities = forms.ModelMultipleChoiceField(
        label='测震设备列表',
        queryset=SeismicEquipmentEntity.objects.all(),
        required=True,
        widget=autocomplete.ModelSelect2Multiple(
            url=reverse_lazy('equipment:seisentity-autocomplete')
        )
    )

    class Meta:
        model = EquipmentList
        fields = ('seismic_equipment_entities',)

    def __init__(self, *args, **kwargs):
        super(EquipmentListForm, self).__init__(*args, **kwargs)

        instance = kwargs.pop('instance', None)

        if instance:
            old_seismic_equipments_id = instance.seismic_equipment_entities.values_list('id', flat=True)

            self.fields['seismic_equipment_entities'].initial = old_seismic_equipments_id
            self.fields['seismic_equipment_entities'].widget.url = reverse_lazy('equipment:seisentity-autocomplete',
                                                                                kwargs={'fk': instance.pk})
